home *** CD-ROM | disk | FTP | other *** search
- Path: news.csuohio.edu!usenet
- From: jabaker@grail.cba.csuohio.edu (jason)
- Newsgroups: comp.lang.c++,comp.lang.c,comp.object,comp.software-eng
- Subject: Re: Pointers are hacks in c++ (portablity hackers etc)
- Date: 25 Mar 1996 12:14:38 -0500
- Organization: Dr Benway for Islam Inc.
- Sender: jason@jlbaker.async.csuohio.edu
- Message-ID: <vpwa1m6wu.fsf_-_@jlbaker.async.csuohio.edu>
- References: <4ikb6kINN1is@mayne.ugrad.cs.ubc.ca> <DoI5Ao.AyJ@assip.csasyd.oz>
- <EJH.96Mar19163745@larry.gsfc.nasa.gov>
- NNTP-Posting-Host: jlbaker.async.csuohio.edu
- In-reply-to: ejh@larry.gsfc.nasa.gov's message of 19 Mar 1996 21:37:45 GMT
- X-Newsreader: Gnus v5.0.13
-
- In article <EJH.96Mar19163745@larry.gsfc.nasa.gov> ejh@larry.gsfc.nasa.gov (Edward Hartnett) writes:
- > Hmmm. Most of what I would call hacked code is not code that violates
- > the standard by depending on undefined behaviour, but rather code that
- > does weird (but usually standard conforming) things with memory or
- > pointers.
-
- What exactly constitutes pointer or memory weirdness? I think that
- going through a list like:
- list_cell **cell = &head;
- while (*cell && (*cell)->member != value) cell = &cell->next;
- looks sort of weird, but is a well established practice. XtOffset and
- stl's list cell management both do weird things, but encapsulate the
- gory details, and presumably work as intended. Are these evil hacks?
-
- I'm posting because I think I see a disturbing trend in c++.
- Programmers seem to have an unreasoning fear of pointers in general:
- An stl object lives in exactly one container, exactly one auto_ptr
- points at any object and reference counting mechanisms seem aimed at
- efficiently giving you a private object rather than sharing an object
- without leakage. With these constructs, you can have only one
- reference to a new'ed object, something I find antithetical to the
- very notion of a pointer.
-
- What is going on here? One explanation is that c++ people do not like
- destructive modifications. However, this does not seem to be the
- case. Stl does not allow you to functional push new cells on the
- front of a list, not does it allow you to functionally push new
- associations on the front of a multimap -- common practice with lisp
- lists and a-lists respectively.
-
- Belief in functional programming really does not explain the
- anti-pointer mood I think I see: What could be more functional and
- safe than building a list of pointers into someone else's container
- and disposing of it before my function exits. Yet Mumit's STL Newbie
- guide (http://www.xraylith.wisc.edu/~khan/software/stl) considers
- collections of pointers "a design flaw that leads to memory leaks and
- such lovely things."
-
- I think c++ programmers fear pointers precisely because C++ does
- nothing to solve the memory management problems of C. Let's say I've
- built my list of pointers on the stack using the following macro:
- #define Stack_new(_T, _ARGS) \
- (void *_v = alloca(sizeof(_T)), new(_v) _T _ARGS)
- How should the list cells be destroyed:
- if next is on the heap and has no other references,
- then delete next.
- if next is on the exiting stack frame and has other references,
- then abort.
- if next is on the stack and has no other references,
- then destroy next.
- if next has other references, or has static extent,
- then do nothing.
- So, there are two questions. How was next allocated -- this can be
- solved by ensuring that next always comes from the heap. And, does
- anything else refer to next -- this can't be solved with a reference
- count, we are dealing with a linked list of arbitrary length, it can
- only be solved by ensuring that there are no other references.
-
- It seems to me that garbage collection is the only solution here.
- But, c++ has avoided gc in the name of efficiency. Instead, it gives us
- three partial solutions which ostensibly save CPU time and memory
- size: Ensure that only one reference to each object exists by copying
- objects and propagating changes between the copies. Keeping a running
- count of references to an object rather than periodically determine
- whether there are any references. And, think through the memory
- management issues on a case by case basis, just like C.
-
- Hmm, I read the Unix Hater's Manual a few months ago, and I am honestly
- not sure whether I am simply parroting their criticism of c++ or
- actually having thoughts of my own. Anyway, if someone can straighten
- me out about multiple references to objects in c++, or if I simply
- provoke a flame war I will be pleased.
-
- Jason
-
-